package com.woniuxy.controller;
/**
 * @author 曾令忠
 */
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.entity.Focus;
import com.woniuxy.entity.PageBean;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.User;
import com.woniuxy.service.FocusService;
import com.woniuxy.service.MessageService;
import com.woniuxy.util.CommonUtil;

@Controller
@RequestMapping("/focus")
public class FocusController {
	
	@Autowired
	private FocusService foucusService;
	@Autowired
	private MessageService messageService;
	
	@RequestMapping("/find")
	@ResponseBody
	public ResponseEntity<PageBean<Focus>> getFocus(int page){
		ResponseEntity<PageBean<Focus>> re = new ResponseEntity<>();
		User nowUser  = CommonUtil.getCurrentUser();
		try {
			PageBean<Focus> focuses = foucusService.findFocusByPage(nowUser.getId(),page);
			re.setCode(1);
			re.setData(focuses);
		} catch (Exception e) {
			e.printStackTrace();
			re.setCode(2);
		}
		return re;
	}
	
	
	
	@RequestMapping("/cancel")
	@ResponseBody
	public ResponseEntity<Void> cancelFocus(int id){
		ResponseEntity<Void> re = new ResponseEntity<>();
		User nowUser  = CommonUtil.getCurrentUser();
//		User nowUser = new User().setId(1);
		try {
			foucusService.cancel(nowUser.getId(), id);
			re.setCode(1);
		} catch (Exception e) {
			e.printStackTrace();
			re.setCode(2);
		}
		
		return re;
	}
	
	@RequestMapping("/addFocus")
	@ResponseBody
	public ResponseEntity<Void> addFocus(int id){
		ResponseEntity<Void> re = new ResponseEntity<>();
		User nowUser  = CommonUtil.getCurrentUser();
		try {
			foucusService.addFocus(nowUser.getId(), id);
			re.setCode(1);
		} catch (Exception e) {
			e.printStackTrace();
			re.setCode(2);
		}
		
		return re;
	}
	
	@RequestMapping("/sendmsg")
	@ResponseBody
	public ResponseEntity<Void> sendMsg(long aid,String content){
		ResponseEntity<Void> re = new ResponseEntity<>();
//		User nowUser  = CommonUtil.getCurrentUser();
		User nowUser = new User().setId(1);
		try {
			messageService.addMsg(nowUser.getId(), aid, content);
			re.setCode(1);
		} catch (Exception e) {
			e.printStackTrace();
			re.setCode(2);
		}
		return re;
	}
	
	@RequestMapping("/getFriends")
	@ResponseBody
	public ResponseEntity<PageBean<User>> getFriends(int page){
		ResponseEntity<PageBean<User>> re = new ResponseEntity<PageBean<User>>();
		User nowUser  = CommonUtil.getCurrentUser();
//		User nowUser = new User().setId(7);
		try {
			PageBean<User> pb = foucusService.getFriendsByPage(nowUser.getId(), page);
			re.setData(pb);
			re.setCode(1);
		} catch (Exception e) {
			e.printStackTrace();
			re.setCode(2);
		}
		return re;
	}
	
	@RequestMapping("/focusMe")
	@ResponseBody
	public ResponseEntity<PageBean<User>> getFocusMe(int page){
		ResponseEntity<PageBean<User>> re = new ResponseEntity<PageBean<User>>();
		User nowUser  = CommonUtil.getCurrentUser();
		try {
			PageBean<User> pb = foucusService.getFocusMe(nowUser.getId(), page);
			re.setData(pb);
			re.setCode(1);
		} catch (Exception e) {
			e.printStackTrace();
			re.setCode(2);
		}
		return re;
	}
	@RequestMapping("/getFocusStatus")
	@ResponseBody
	public String getFocusStatus(long id) {
		User user = CommonUtil.getCurrentUser();
		String status = foucusService.getFocusStatus(user.getId(),id);
		return status;
		
	}
}
